package com.vf.admin.miaodong.controller.agent;

import com.vf.admin.client.service.*;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import com.vf.admin.miaodong.service.AfterUserService;
import com.vf.admin.utils.StringEx;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/agent")
public class AgentController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppUserService appUserService;
    @Autowired
    AgentUserRecordsService agentUserRecordsService;
    @Autowired
    AfterUserService afterUserService;
    @Autowired
    AgentUserService agentUserService;
    @Autowired
    AgentUserWithdrawService agentUserWithdrawService;
    @Autowired
    AgentUserMoneyRecordService agentUserMoneyRecordService;
    @Autowired
    SsAreaService areaService;
    @Autowired
    AgentUserMessageService agentUserMessageService;

    @RequestMapping("/login")
    public Result login(String loginname, String password) {
        try {
            if (StringUtils.isBlank(loginname)) {
                return this.errorJSON("账号不能为空！");
            }
            if (StringUtils.isBlank(password)) {
                return this.errorJSON("密码不能为空！");
            }
            AgentUser user = agentUserService.findByUser(loginname, password);
            if (user == null || user.getId() == null || user.getId() <= 0) {
                return this.errorJSON("该用户不存在或密码错误！");
            }
            SsArea ssArea = areaService.get(user.getAreaId());
            if(ssArea != null) {
                user.setAreaName(ssArea.getName());
            }
            return sucessJSON(user, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return errorJSON("error");
        }
    }

    @RequestMapping("/index")
    public Result index(Integer userId) {
        Map<String, Object> data = new HashMap<>();
        int allUser = 0, allUserMan = 0, allUserWoman = 0, newUser = 0, newUserMan = 0, newUserWoman = 0;
        BigDecimal rechargeMoneyToday = BigDecimal.ZERO, withdrawMoneyToday = BigDecimal.ZERO, moneyToday = BigDecimal.ZERO;

        List<AgentUserRecords> list = agentUserRecordsService.list(userId);
        allUser = list.size();
        allUserMan = (int) list.stream().filter(e -> e.getSex() == 1).count();
        allUserWoman = (int) list.stream().filter(e -> e.getSex() == 2).count();

        List<AgentUserRecords> list1 = agentUserRecordsService.todayNewUser(userId);
        newUser = list1.size();
        newUserMan = (int) list1.stream().filter(e -> e.getSex() == 1).count();
        newUserWoman = (int) list1.stream().filter(e -> e.getSex() == 2).count();

        String subUserIds = agentUserRecordsService.subUserId(userId);
        rechargeMoneyToday = agentUserRecordsService.rechargeToday(subUserIds);
        withdrawMoneyToday = agentUserRecordsService.withdrawToday(subUserIds);
        moneyToday = agentUserMoneyRecordService.todayMoney(userId);

        AgentUser user = agentUserService.get(userId);

        data.put("allUser", allUser);
        data.put("allUserMan", allUserMan);
        data.put("allUserWoman", allUserWoman);
        data.put("newUser", newUser);
        data.put("newUserMan", newUserMan);
        data.put("newUserWoman", newUserWoman);
        data.put("rechargeMoneyToday", rechargeMoneyToday);
        data.put("withdrawMoneyToday", withdrawMoneyToday);
        data.put("money", user.getMoney());
        data.put("moneyToday", moneyToday);
        return sucessJSON(data);
    }

    @RequestMapping("/userList")
    public Result getAllUser(Integer userId, SplitPageBean splitPage) {
        try {
            Map<String, Object> data = new HashMap<>();
            Map<String, Object> map = new HashMap<>();

            map.put("parentUserId", userId);
            List<AppUser> maps = afterUserService.findList1(map, splitPage);

            data.put("list", maps);
            data.put("page", splitPage);
            return sucessJSON(data);
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/withdraw")
    public Result withdraw(Integer userId, BigDecimal money) {
        try {
            BigDecimal money1 = agentUserService.getMoney(userId);
            if(money1.compareTo(BigDecimal.ZERO) == 0) {
                return errorJSON("余额不足");
            }
            if (money1.compareTo(money) >= 0) {
                AgentUserWithdraw param = new AgentUserWithdraw();
                param.setAgentUserId(userId);
                param.setMoney(money);
                param.setStatus(0);
                param.setCreateTime(new Date());
                agentUserWithdrawService.withdraw(param);
                return sucessJSON("成功");
            } else {
                return errorJSON("余额不足");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/messageList")
    public Result userList(SplitPageBean splitPageBean, Integer userId) {
        try {
            Map<String, Object> data = new HashMap<>();
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("userId", userId);
            List<AgentUserMessage> list = agentUserMessageService.page(paramMap, splitPageBean);
            if(list.size() > 0) {
                agentUserMessageService.readMessage(list.stream().map(e -> e.getId().toString()).collect(Collectors.joining(",")));
            }
            data.put("list", list);
            data.put("page", splitPageBean);
            return sucessJSON(data);
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙");
        }
    }
}
